package com.catgod007.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.catgod007.yudada.model.dto.question.QuestionContentDTO;
import com.catgod007.yudada.model.entity.App;
import com.catgod007.yudada.model.entity.Question;
import com.catgod007.yudada.model.entity.ScoringResult;
import com.catgod007.yudada.model.entity.UserAnswer;
import com.catgod007.yudada.model.vo.QuestionVO;
import com.catgod007.yudada.service.QuestionService;
import com.catgod007.yudada.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义测评类应用策略
 */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)//测评类-自定义类
public class CustomTestScoringStrategyImpl implements ScoringStrategy {
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        Long appId = app.getId();
        //1. 根据id查询到题目和题目结果信息
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId)
        );

        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class).eq(ScoringResult::getAppId, appId)
        );
        //2.统计用户每个选择对应的属性个数，如I=10个，E=8个
        Map<String,Integer> optionsCount = new HashMap<>();

        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        //遍历题目列表
        for (QuestionContentDTO questionContentDTO : questionContent) {
            //遍历答案列表
            for (String answer : choices){
                //题目中的选项
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    //如果答案和选项的key匹配
                    if (answer.equals(option.getKey())) {
                        //获取选项的result属性
                        String result = option.getResult();
                    //如果result不在optionsCount中，初始化为0
                    if (!optionsCount.containsKey(result)) {
                        optionsCount.put(result, 0);
                    }
                    //将result对应的值加1
                    optionsCount.put(result, optionsCount.get(result) + 1);
                    }
                }
            }
        }
        //3.遍历每种评分结果，计算哪个结果的得分更高
        int maxScore = 0;
        ScoringResult maxScoringResult = scoringResultList.get(0);
        //遍历评分结果列表
        for (ScoringResult scoringResult : scoringResultList) {
            List<String> resultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
            // 计算当前评分结果的分数，[I,E] ==>[10,5] ==> 15
            int score = resultProp.stream()
                    .mapToInt(prop -> optionsCount.getOrDefault(prop, 0)).sum();
            // 如果当前得分比最大得分高，更新最大得分和最大评分结果
            if (score > maxScore) {
                maxScore = score;
                maxScoringResult = scoringResult;
            }
        }
        //4.构造返回值，填充对象数属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());

        return userAnswer;
    }
}
